package presentation.optionForm_UI;

import business.optionForm_Evt.Statics_Evt;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class StaticsForm_UI extends JDialog {

	private JPanel				panelLabel			= new JPanel();
	private JPanel				panelButtons		= new JPanel();
	private JPanel				panelMain			= new JPanel();
	private Statics_Evt			event				= new Statics_Evt(this);
	private JButton				btClose				= new JButton("Close");
	private JButton				btClear				= new JButton("Clear");

	private JLabel				lbMaxLength			= new JLabel("Max. Length:  ", JLabel.LEFT);
	private JLabel				lbBestWord			= new JLabel("Best Word:  ", JLabel.LEFT);
	private JLabel				lbMaxPoint			= new JLabel("Max Point:  ", JLabel.LEFT);
	private JLabel				lbWpm				= new JLabel("Char/min:  ", JLabel.LEFT);
	private JLabel				lbTotalTime			= new JLabel("Total time:  ", JLabel.LEFT);
	private JLabel				lbAvgLength			= new JLabel("Avg. Length:  ", JLabel.LEFT);
	private JLabel				lbWordFound			= new JLabel("Word Found:  ", JLabel.LEFT);
	private JLabel				lbMaxLevel			= new JLabel("MaxLevel:  ", JLabel.LEFT);
	private JLabel				lbHighScore			= new JLabel("HighScore:  ", JLabel.LEFT);

	private JLabel				lbrMaxLength		= new JLabel(0 + "", JLabel.LEFT);
	private JLabel				lbrBestWord			= new JLabel(0 + "", JLabel.LEFT);
	private JLabel				lbrMaxPoint			= new JLabel(0 + "", JLabel.LEFT);
	private JLabel				lbrWpm				= new JLabel(0 + "", JLabel.LEFT);
	private JLabel				lbrTotalTime		= new JLabel(0 + "", JLabel.LEFT);
	private JLabel				lbrAvgLength		= new JLabel(0 + "", JLabel.LEFT);
	private JLabel				lbrWordFound		= new JLabel(0 + "", JLabel.LEFT);
	private JLabel				lbrMaxLevel			= new JLabel(0 + "", JLabel.LEFT);
	private JLabel				lbrHighScore		= new JLabel(0 + "", JLabel.LEFT);

	private static final long	serialVersionUID	= 1L;

	public StaticsForm_UI(JDialog owner) {
		super(owner, true);
		initComponents();

		this.setSize(448, 443);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(owner);
		this.setResizable(false);
		this.setTitle("Statics");

		buttonEvent();
		event.setTextComponent(lbrMaxLength, lbrBestWord, lbrMaxPoint, lbrWpm, lbrTotalTime, lbrAvgLength, lbrWordFound, lbrMaxLevel, lbrHighScore);
	}

	private void initComponents() {
		setMainLayout();
	}

	private void setMainLayout() {
		setPanelLabelLayout();
		setPanelButtonsLayout();
		this.add(panelMain);

		panelMain.setLayout(null);

		panelMain.add(panelLabel);
		panelMain.add(panelButtons);

		panelLabel.setBounds(1, 1, 440, 340);
		panelButtons.setBounds(panelLabel.getX(), panelLabel.getY() + panelLabel.getHeight() + 3, panelLabel.getWidth(), 410 - panelLabel.getHeight());

	}

	private void setPanelLabelLayout() {
		setLabel();

		panelLabel.setLayout(null);
		panelLabel.setBorder(BorderFactory.createTitledBorder(""));

		panelLabel.add(lbMaxLength);
		panelLabel.add(lbrMaxLength);
		
		panelLabel.add(lbBestWord);
		panelLabel.add(lbrBestWord);
		
		panelLabel.add(lbMaxPoint);
		panelLabel.add(lbrMaxPoint);
		
		panelLabel.add(lbWpm);
		panelLabel.add(lbrWpm);
		
		panelLabel.add(lbTotalTime);
		panelLabel.add(lbrTotalTime);
		
		panelLabel.add(lbAvgLength);
		panelLabel.add(lbrAvgLength);
		
		panelLabel.add(lbWordFound);
		panelLabel.add(lbrWordFound);
		
		panelLabel.add(lbMaxLevel);
		panelLabel.add(lbrMaxLevel);
		
		panelLabel.add(lbHighScore);
		panelLabel.add(lbrHighScore);
		

		lbMaxLength.setBounds(42, 30, 150, 30);
		lbrMaxLength.setBounds(lbMaxLength.getX() + lbMaxLength.getWidth() + 20, lbMaxLength.getY(), 200, lbMaxLength.getHeight());

		lbBestWord.setBounds(lbMaxLength.getX(), lbMaxLength.getY() + lbMaxLength.getHeight() + 5, lbMaxLength.getWidth(), lbMaxLength.getHeight());
		lbrBestWord.setBounds(lbBestWord.getX() + lbBestWord.getWidth() + 20, lbBestWord.getY(), lbrMaxLength.getWidth(), lbMaxLength.getHeight());

		lbMaxPoint.setBounds(lbBestWord.getX(), lbBestWord.getY() + lbBestWord.getHeight() + 5, lbMaxLength.getWidth(), lbMaxLength.getHeight());
		lbrMaxPoint.setBounds(lbMaxPoint.getX() + lbMaxPoint.getWidth() + 20, lbMaxPoint.getY(), lbrMaxLength.getWidth(), lbMaxLength.getHeight());

		lbWpm.setBounds(lbMaxPoint.getX(), lbMaxPoint.getY() + lbMaxPoint.getHeight() + 5, lbMaxLength.getWidth(), lbMaxLength.getHeight());
		lbrWpm.setBounds(lbWpm.getX() + lbWpm.getWidth() + 20, lbWpm.getY(), lbrMaxLength.getWidth(), lbMaxLength.getHeight());

		lbTotalTime.setBounds(lbWpm.getX(), lbWpm.getY() + lbWpm.getHeight() + 5, lbMaxLength.getWidth(), lbMaxLength.getHeight());
		lbrTotalTime.setBounds(lbTotalTime.getX() + lbTotalTime.getWidth() + 20, lbTotalTime.getY(), lbrMaxLength.getWidth(), lbMaxLength.getHeight());

		lbWordFound.setBounds(lbTotalTime.getX(), lbTotalTime.getY() + lbTotalTime.getHeight() + 5, lbMaxLength.getWidth(), lbMaxLength.getHeight());
		lbrWordFound.setBounds(lbWordFound.getX() + lbWordFound.getWidth() + 20, lbWordFound.getY(), lbrMaxLength.getWidth(), lbMaxLength.getHeight());
		
		lbMaxLevel.setBounds(lbWordFound.getX(), lbWordFound.getY() + lbWordFound.getHeight() + 5, lbMaxLength.getWidth(), lbMaxLength.getHeight());
		lbrMaxLevel.setBounds(lbMaxLevel.getX() + lbMaxLevel.getWidth() + 20, lbMaxLevel.getY(), lbrMaxLength.getWidth(), lbMaxLength.getHeight());
		
		lbHighScore.setBounds(lbMaxLevel.getX(), lbMaxLevel.getY() + lbMaxLevel.getHeight() + 5, lbMaxLength.getWidth(), lbMaxLength.getHeight());
		lbrHighScore.setBounds(lbHighScore.getX() + lbHighScore.getWidth() + 20, lbHighScore.getY(), lbrMaxLength.getWidth(), lbMaxLength.getHeight());
	}

	private void setLabel() {
		lbMaxLength.setFont(new Font("Comic Sans MS", 0, 19));
		lbrMaxLength.setFont(new Font("Comic Sans MS", 0, 19));
		
		lbBestWord.setFont(new Font("Comic Sans MS", 0, 19));
		lbrBestWord.setFont(new Font("Comic Sans MS", 0, 19));
		
		lbMaxPoint.setFont(new Font("Comic Sans MS", 0, 19));
		lbrMaxPoint.setFont(new Font("Comic Sans MS", 0, 19));
		
		lbWpm.setFont(new Font("Comic Sans MS", 0, 19));
		lbrWpm.setFont(new Font("Comic Sans MS", 0, 19));
		
		lbTotalTime.setFont(new Font("Comic Sans MS", 0, 19));
		lbrTotalTime.setFont(new Font("Comic Sans MS", 0, 19));
		
		lbAvgLength.setFont(new Font("Comic Sans MS", 0, 19));
		lbrAvgLength.setFont(new Font("Comic Sans MS", 0, 19));
		
		lbWordFound.setFont(new Font("Comic Sans MS", 0, 19));
		lbrWordFound.setFont(new Font("Comic Sans MS", 0, 19));
		
		lbMaxLevel.setFont(new Font("Comic Sans MS", 0, 19));
		lbrMaxLevel.setFont(new Font("Comic Sans MS", 0, 19));
		
		lbHighScore.setFont(new Font("Comic Sans MS", 0, 19));
		lbrHighScore.setFont(new Font("Comic Sans MS", 0, 19));
	}

	private void setPanelButtonsLayout() {
		setButtons();

		panelButtons.setLayout(new GridBagLayout());
		panelButtons.setBorder(BorderFactory.createTitledBorder(""));

		GridBagConstraints cOk = new GridBagConstraints();
		cOk.gridx = 0;
		cOk.gridy = 0;
		cOk.insets = new Insets(0, 0, 0, 20);
		panelButtons.add(btClear, cOk);

		GridBagConstraints cClear = new GridBagConstraints();
		cClear.gridx = 1;
		cClear.gridy = 0;
		panelButtons.add(btClose, cClear);

	}

	private void setButtons() {
		btClose.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));
		btClose.setPreferredSize(new Dimension(90, 40));
		btClose.setFont(new Font("Comic Sans MS", 0, 19));

		btClear.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1, true));
		btClear.setPreferredSize(new Dimension(90, 40));
		btClear.setFont(new Font("Comic Sans MS", 0, 19));
	}

	private void buttonEvent() {
		btClear.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				event.btClear_Evt(lbrMaxLength, lbrBestWord, lbrMaxPoint, lbrWpm, lbrTotalTime, lbrAvgLength, lbrWordFound, lbrMaxLevel, lbrHighScore);
			}
		});

		btClose.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				event.btClose_Evt();
			}
		});

		btClear.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_LEFT || evt.getKeyCode() == KeyEvent.VK_RIGHT) {
					btClose.requestFocus();
				}
				if (evt.getKeyCode() == KeyEvent.VK_UP || evt.getKeyCode() == KeyEvent.VK_DOWN) {
					btClose.requestFocus();
				}
				if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
					dispose();
				}
			}
		});

		btClose.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_LEFT || evt.getKeyCode() == KeyEvent.VK_RIGHT) {
					btClear.requestFocus();
				}
				if (evt.getKeyCode() == KeyEvent.VK_UP || evt.getKeyCode() == KeyEvent.VK_DOWN) {
					btClear.requestFocus();
				}
				if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
					dispose();
				}
			}
		});
	}
}
